Largest unique number

Time: O(N); Space: O(N); easy

Given an array of integers nums, return the largest integer that only occurs once.

If no integer occurs once, return -1.

Example 1:

Input: nums = [5,7,3,9,4,9,8,3,1]

Output: 8

Explanation:

  • The maximum integer in the array is 9 but it is repeated. The number 8 occurs only once, so it’s the answer.

Example 2:

Input: nums = [9,9,8,8]

Output: -1

Explanation:

  • There is no number that occurs only once.

Constraints:

  • 1 <= len(A) <= 2000

  • 0 <= A[i] <= 1000

[3]:
import collections

class Solution1(object):
    """
    Time: O(N)
    Space: O(N)
    """
    def largestUniqueNumber(self, nums):
        """
        :type nums: List[int]
        :rtype: int
        """
        nums.append(-1)
        return max(k for k,v in collections.Counter(nums).items() if v == 1)
[4]:
s = Solution1()

nums = [5,7,3,9,4,9,8,3,1]
assert s.largestUniqueNumber(nums) == 8

nums = [9,9,8,8]
assert s.largestUniqueNumber(nums) == -1